﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Amgine
{
    partial class AccountEditForm : Form
    {
        private Account account;
        private List<AccountGroup> groupList;
        private List<Dictionary> dictionaryList;

        public AccountEditForm(Account account)
        {
            InitializeComponent();
            this.account = (account == null ? new Account() : account);
            groupList = DBHelper.listAllAccountGroup();
            dictionaryList = DBHelper.listAllDictionary();
        }

        private void AccountEditForm_Load(object sender, EventArgs e)
        {
            int position = -1;
            int count = 0;
            foreach (AccountGroup group in groupList)
            {
                comboBox_group.Items.Add(group.Title);
                if (group.ID == account.GroupId)
                {
                    position = count;
                }
                count++;
            }
            comboBox_group.SelectedIndex = position;

            textBox_title.Text = account.Title;
            comboBox_name.Items.Add(account.Name);
            comboBox_password.Items.Add(account.Password);
            textBox_remark.Text = account.Remark;

            foreach (Dictionary dictionary in dictionaryList)
            {
                switch (dictionary.Type)
                {
                    case Dictionary.TYPE_ACCOUNT:
                        comboBox_name.Items.Add(dictionary.Code);
                        break;
                    case Dictionary.TYPE_PASSWORD:
                        comboBox_password.Items.Add(dictionary.Code);
                        break;
                    default: break;
                }
            }
        }

        private void button_confirm_Click(object sender, EventArgs e)
        {
            account.Title = textBox_title.Text;
            account.Name = comboBox_name.Text;
            account.Password = comboBox_password.Text;
            account.Remark = textBox_remark.Text;

            if (comboBox_group.SelectedIndex == -1)
            {
                MessageBox.Show("请选择所属分组", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            account.GroupId = groupList.ElementAt(comboBox_group.SelectedIndex).ID;

            if (validateData())
            {
                if (account.createOrUpdate())
                {
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    DialogResult = DialogResult.Retry;
                }
                Close();
            }
        }

        private bool validateData()
        {
            if (string.IsNullOrWhiteSpace(account.Title))
            {
                MessageBox.Show("名称不可为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else if (string.IsNullOrWhiteSpace(account.Name))
            {
                MessageBox.Show("登录名不可为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else if (string.IsNullOrWhiteSpace(account.Password))
            {
                MessageBox.Show("密码不可为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrWhiteSpace(account.Remark))
            {
                MessageBox.Show("备注不可为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
